package fr.mairie.parc.dao.impl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import fr.mairie.parc.dao.AttributionDao;
import fr.mairie.parc.dao.mapper.AttributionRowMapper;
import fr.mairie.parc.domain.Attribution;

public class AttributionDaoImpl implements AttributionDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Autowired
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	public JdbcTemplate getJdbcTempalete() {
		return jdbcTemplate;
	}

	@Autowired
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Attribution> selectAll() {
		return jdbcTemplate.query("SELECT idAttribution, idDemande, idUser, idMateriel" +
				"" + " FROM attribution",
						new AttributionRowMapper());
	}

	@Override
	public void addAttribution(Attribution attribution) {
		jdbcTemplate.update("INSERT INTO attribution (idDemande, idUser, idMateriel) values (?,?,?)",
				new Object [] {
							   attribution.getIdDemande(),
							   attribution.getIdUser(),
							   attribution.getIdMateriel()
							   
							   });
		
	}

	@Override
	public Attribution getAttributionById(int id) {
		return (Attribution) jdbcTemplate.queryForObject("SELECT * FROM attribution WHERE idAttribution=?",
				new Object [] {id},
				new AttributionRowMapper());
	}

	@Override
	public void updateAttribution(Attribution attribution) {
		jdbcTemplate.update("UPDATE attribution SET idMateriel = ? " +
				" WHERE idAttribution = ? ",
                new Object [] {attribution.getIdMateriel()
							   
							   
				});
		
	}

	@Override
	public void deleteAttribution(Attribution attribution) {
		jdbcTemplate.update("DELETE FROM attribution WHERE idAttribution= ?", 
				new Object[] {attribution.getIdAttribution()});
		
	}

}
